﻿using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SK3000RunningTestTool.SKServerSource
{
    public class ServiceTestSource
    {
        public static List<ISKService> ServiceTestList = new List<ISKService>();

        public static bool Init()
        {
            bool bolResult = false;
            ServiceTestList = new List<ISKService>();
            XmlNodeList lstNode = CommonMethod.XmlUse.GetNodeListInfo(Parameter.XMLDataFilePath, Parameter.XMLParentNodeName, Parameter.XMLNodeName);
            foreach (XmlNode node in lstNode)
            {
                ISKService result;
                Enum_ServiceTestType typeId = (Enum_ServiceTestType)Convert.ToInt32(node.Attributes["TypeID"].Value);
                switch (typeId)
                {
                    case Enum_ServiceTestType.TCPService:
                        result = new Service_TCPService();
                        break;
                    case Enum_ServiceTestType.ATUDSerice:
                        result = new Service_ATDU();
                        break;
                    case Enum_ServiceTestType.DeviceService:
                        result = new Service_Device();
                        break;
                    case Enum_ServiceTestType.VDTUService:
                        result = new Service_VTDU();
                        break;
                    case Enum_ServiceTestType.VideoRecordService:
                        result = new Service_VideoRecord();
                        break;
                    case Enum_ServiceTestType.WebService:
                        result = new Service_WebService();
                        break;
                    case Enum_ServiceTestType.WebSocket:
                        result = new Service_WebSocket();
                        break;
                    default:
                        continue;
                }
                result.ID = Convert.ToInt32(node.Attributes["ID"].Value);
                result.ServiceAddress = Convert.ToString(node.Attributes["ServiceAddress"].Value);
                result.ServicePort = Convert.ToUInt16(node.Attributes["ServicePort"].Value);
                result.ServiceName = node.Attributes["ServiceName"].Value;
                result.ServiceRemark= node.Attributes["ServiceRemark"].Value;
                ServiceTestList.Add(result);
            }
            return bolResult;
        }

        public static int GetID()
        {
            int intResult = 1;
            if (ServiceTestList.Count > 0)
            {
                intResult = ServiceTestList.Max(i => i.ID) + 1;
            }
            return intResult;
        }
    }
}
